﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    public class Player : ISprite
    {
        #region variabele declaratie

        public string loopDirectie = "stilstaan";
        public Texture2D texture;

        public GraphicsDevice graphics { get; set; }

        public SpriteBatch spriteBatch { get; set; }

        public ContentManager content { get; set; }

        public Vector2 tempCurrentFrame;
        public Vector2 playerPosition;

        public Vector2 position { get; set; }

        public float moveSpeed = 0.5f;
        private Animation playerAnimation = new Animation();
        public bool endRechts = false;
        public bool endLinks = false;
        private KeyboardState keyState;
        private KeyboardState PreviousKeyState;
        private bool eersteKeer = true;
        private bool magPlayerVerderSpringen = true;
        private bool jumping = false, falling = false;
        private int maxhoogte;
        float moveSpeedJump = 5;
        public int grondhoogte = 425;
        public bool doorlopen = true;
        public bool magRechts = true;
        public bool magLinks = true;

        #endregion variabele declaratie

        public Player(ContentManager content, Vector2 startPosition)
        {
            this.content = content;
            playerPosition = startPosition;
            playerAnimation.Initialize(playerPosition, new Vector2(4, 2));
            tempCurrentFrame = Vector2.One;
            Load();
        }

        public void Load()
        {
            texture = content.Load<Texture2D>("AlgemeneTextures/ClemensSprite");
            playerAnimation.AnimationImage = texture;
        }

        public void Links()
        {
            if (magLinks)
            {
                playerPosition.X -= moveSpeed;
                tempCurrentFrame.Y = 0;
                loopDirectie = "links";
                endLinks = false;
            }
        }
        public void Rechts()
        {
            if (magRechts)
            {
                playerPosition.X += moveSpeed;
                loopDirectie = "rechts";
                endRechts = false;
                tempCurrentFrame.Y = 1;
            }
        }

        public void Update(GameTime gameTime)
        {
            keyState = Keyboard.GetState();
            playerAnimation.Active = true;
            PreviousKeyState = keyState;

            #region zorgen dat de animatie blijft werken
            if (keyState.IsKeyDown(Keys.Left))
            {
                if (playerPosition.X < 50)
                {
                    endLinks = true;
                }
                
            }
            else if (keyState.IsKeyDown(Keys.Right) && doorlopen == true)
            {
                if (playerPosition.X + texture.Width > 770)
                {
                    endRechts = true;
                }
            }
            else
            {
                playerAnimation.Active = false;
                loopDirectie = "stilstaan";
            }
            #endregion

            maxhoogte = BerekenMaxHoogte();

            if (playerPosition.Y < maxhoogte || moveSpeedJump <= 0)
            {
                magPlayerVerderSpringen = false;
            }
            
           

            tempCurrentFrame.X = playerAnimation.CurrentFrame.X;
            playerAnimation.Position = playerPosition;
            playerAnimation.CurrentFrame = tempCurrentFrame;
            playerAnimation.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            playerAnimation.Draw(spriteBatch);
        }

        private int BerekenMaxHoogte()
        {
            if (eersteKeer == true)
            {
                int hoogte = (int)playerPosition.Y - (int)150;
                eersteKeer = false;
                return hoogte;
            }
            else
            {
                return maxhoogte;
            }
        }
        public void Jump()
        {
            moveSpeedJump = moveSpeedJump - 0.06f;
            if (moveSpeedJump > 0)
            {
                playerPosition.Y -= moveSpeedJump;
            }
        }
        public void Fall()
        {            
            playerPosition.Y += moveSpeed * 4;
        }
        public void BovenCollision(bool bovencollision)
        {
            if (bovencollision)
            {
                grondhoogte = (int)playerPosition.Y;
            }
            if (!bovencollision)
            {
                grondhoogte = 425;
            }
        }
        public void Springen(bool up)
        {
            if (up == true && magPlayerVerderSpringen == true)
            {
                Jump();
                jumping = true;
                falling = false;
            }
            else
            {
                jumping = false;
            }

            if (playerPosition.Y < grondhoogte && magPlayerVerderSpringen == false)
            {
                Fall();
                eersteKeer = true;
                jumping = false;
                falling = true;
            }
            else
            {
                falling = false;
            }

            if (jumping == false && falling == false)
            {
                eersteKeer = true;
            }

            float tussenligging = playerPosition.Y - grondhoogte;

            if (tussenligging < 5 && tussenligging > -5)
            {
                magPlayerVerderSpringen = true;
                moveSpeedJump = 5;
            }

            if (playerPosition.Y < grondhoogte && jumping == false && falling == false)
            {
                Fall();
                eersteKeer = true;
                magPlayerVerderSpringen = false;
            }
        }
    }    
}
